trigger Attachment_InUse on Attachment (before delete) {
List<ID> batchIdList = new list<ID>();
Map<ID,String> batchNamesMap = new Map<ID, String>();
Map<ID,String> batchStatusMap = new Map<ID, String>();

String bName = '';
Boolean deleteAttachFlag = true;
Boolean flag = false;

    if(Trigger.isDelete){
        for(Attachment a: Trigger.old){
            Template__c[] t = [select Id from Template__c t where t.Id = :a.ParentId];
            if(t.size() > 0)
            {
                String BatchNames = '';
                for(Batch__c[] b : [Select Id, Name, Status__c from Batch__c b where b.Template__c = :t[0].Id])
                {
                    for(Batch__c batch: b)
                    {
                        batchIdList.add(batch.Id);

                        if(batch.Status__c == 'Distributed')
                            batchStatusMap.put(batch.Id, batch.Status__c);

                        if(batch.Status__c == 'Completed')
                            batchNamesMap.put(batch.Id, batch.Name);
                        
                        if(batchIdList.size() == b.size())
                        {
                            flag = false;
                            for(Sales_Contract__c[] sc : [select Id, BatchID__c from Sales_Contract__c where BatchID__c in :batchIdList and AttachmentID__c = :a.Id])
                            {
                                if(sc.size() > 0)
                                {
                                    flag = true;
                                    for(Sales_Contract__c sContract: sc)
                                    {
                                        
                                        if(batchStatusMap.get(sContract.BatchID__c) == 'Distributed')
                                            deleteAttachFlag = false;
                                    
                                        bName = batchNamesMap.get(sContract.BatchID__c);
                                        if(bName != null)
                                            BatchNames += bName+'\n';

                                    }
                                }
                                
                            }
                            
                            if(flag == false)
                            {
                                Attachment[] att = [select Name, Body from Attachment where Id = :a.Id];
                                String body = att[0].Body.toString();
                                Integer indexOfBody = body.indexOf('<body', 0);
                                if(indexOfBody == -1)
                                    indexOfBody = body.indexOf('<Body', 0);
                
                                if(indexOfBody == -1)
                                    indexOfBody = body.indexOf('<BODY', 0);
                                
                                String extractedBody = '';
                                if(indexOfBody != -1)
                                {
                                    extractedBody = body.substring(indexOfBody);
                                
                                    Integer indexOfLFB = extractedBody.indexOf('{', 0);
                                    Integer indexOfRFB = extractedBody.indexOf('}', 0);
                                    
                                    if(indexOfLFB == -1 && indexOfRFB == -1)
                                        deleteAttachFlag = false;
                                }

                                if(att[0].Name.indexOf('.doc') != -1)
                                    deleteAttachFlag = false;
                            }                                

                            batchIdList.clear();
                        }//end of if(batchIdList.size() == ....
                           
                    }
                }
                if(deleteAttachFlag == false)
                    a.addError('You can\'t delete this Attachment.');
                else if(BatchNames != '')
                    a.addError('The Attachment can not be deleted as the below batches are using it.\n'+BatchNames);
            }
        }
    }
}


/*
trigger Attachment_InUse on Attachment (before delete) {
List<ID> batchIdList = new list<ID>();
Map<ID,String> batchNamesMap = new Map<ID, String>();
Map<ID,String> batchStatusMap = new Map<ID, String>();

String bName = '';
Boolean deleteAttachFlag = true;
Boolean flag = false;

    if(Trigger.isDelete){
        for(Attachment a: Trigger.old){
            Template__c[] t = [select Id from Template__c t where t.Id = :a.ParentId];
            if(t.size() > 0)
            {
                String BatchNames = '';
                for(Batch__c[] b : [Select Id, Name, Status__c from Batch__c b where b.Template__c = :t[0].Id])
                {
                    for(Batch__c batch: b)
                    {
                        batchIdList.add(batch.Id);

                        if(batch.Status__c == 'Distributed')
                            batchStatusMap.put(batch.Id, batch.Status__c);

                        if(batch.Status__c == 'Completed')
                            batchNamesMap.put(batch.Id, batch.Name);
                        
                        if(batchIdList.size() == b.size())
                        {
                            flag = false;
                            for(Sales_Contract__c[] sc : [select Id, BatchID__c from Sales_Contract__c where BatchID__c in :batchIdList and AttachmentID__c = :a.Id])
                            {
                                if(sc.size() > 0)
                                {
                                    flag = true;
                                    for(Sales_Contract__c sContract: sc)
                                    {
                                        
                                        if(batchStatusMap.get(sContract.BatchID__c) == 'Distributed')
                                            deleteAttachFlag = false;
                                    
                                        bName = batchNamesMap.get(sContract.BatchID__c);
                                        if(bName != null)
                                            BatchNames += bName+'\n';

                                    }
                                }
                                
                            }
                            
                            if(flag == false)
                            {
                                Attachment[] att = [select Body from Attachment where Id = :a.Id];
                                String body = att[0].Body.toString();
                                Integer indexOfBody = body.indexOf('<body', 0);
                                if(indexOfBody == -1)
                                    indexOfBody = body.indexOf('<Body', 0);
                
                                if(indexOfBody == -1)
                                    indexOfBody = body.indexOf('<BODY', 0);
                                
                                String extractedBody = '';
                                if(indexOfBody != -1)
                                {
                                    extractedBody = body.substring(indexOfBody);
                                
                                    Integer indexOfLFB = extractedBody.indexOf('{', 0);
                                    Integer indexOfRFB = extractedBody.indexOf('}', 0);
                                    
                                    if(indexOfLFB == -1 && indexOfRFB == -1)
                                    {
                                        deleteAttachFlag = false;
                                    }
                                }
                            }                                

                            batchIdList.clear();
                        }//end of if(batchIdList.size() == ....
                           
                    }
                }
                if(deleteAttachFlag == false)
                    a.addError('You can\'t delete this Attachment.');
                else if(BatchNames != '')
                    a.addError('The Attachment can not be deleted as the below batches are using it.\n'+BatchNames);
            }
        }
    }
}
*/

/*trigger Attachment_InUse on Attachment (before delete) {
    if(Trigger.isDelete){
        for(Attachment a: Trigger.old){
            Template__c[] t = [select Id from Template__c t where t.Id = :a.ParentId];
            if(t.size() > 0)
            {
                String BatchNames = '';
                for(Batch__c b : [Select Name, Status__c from Batch__c b where b.Template__c = :t[0].Id])
                {
                  if(b.Status__c == 'Published')
                      a.addError('You can\'t delete this Attachment.');
                  if(b.Status__c == 'Completed')
                      BatchNames += b.Name+'\n';
                }
                if(BatchNames != '')
                    a.addError('The Attachment can not be deleted as the below batches are using it.\n'+BatchNames);
            }
        }
    }
}*/